#include "texteditor.h"
#include "ui_texteditor.h"
#include"finddlg.h"

#include<QFile>
#include<QMessageBox>
#include<QTextStream>
#include<QFileInfo>
#include<QFileDialog>
#include<QTextStream>
#include<QToolBar>
#include<QDateTime>
TextEditor::TextEditor(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::TextEditor)
{
    ui->setupUi(this);
    this->setWindowTitle(tr("Text Editor"));
    this->findDlg = NULL;
}

TextEditor::~TextEditor()
{
    delete ui;
}

bool TextEditor::okToContinue()
{
    if(this->isWindowModified())
    {
        int r = QMessageBox::warning(this,tr("Text Editor"),
                                         tr("The Document has been modified.\n"
                                            "Do you want to save your changes?"),
                                         QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
       // if yes is choosed,run "save()",else if cancel is choosed ,false is returned.if no is choosed,true is returned
        if(r == QMessageBox::Yes)
            return save();
        else if(r == QMessageBox::Cancel)
            return false;
    }
    return true;
}
bool TextEditor::save()
{
    if(this->curFile.isEmpty())
        return this->saveAs();
    else
        return this->saveFile(this->curFile);
}
bool TextEditor::saveFile(const QString &fileName)
{
    QFile file(fileName);
    if(!file.open(QFile::WriteOnly | QFile::Text))
    {
        QMessageBox::warning(this,tr("save file"),tr("can't save file %1:\n %2").arg(fileName)
                             .arg(file.errorString()));
        ui->statusBar->showMessage(tr("saving canceled"),2000);
        return false;
    }

        QTextStream out(&file);
        out<<ui->textEdit->toPlainText();
        this->isSaved = true;
        this->curFile = QFileInfo(fileName).canonicalFilePath();
        this->setWindowTitle(curFile);
        ui->statusBar->showMessage(tr("%1 has been saved").arg(this->curFile),2000);
        return true;
}

bool TextEditor::saveAs()
{
    QString fileName = QFileDialog::getSaveFileName(this,tr("save text file"),".",
                                                    tr("text files (*.txt)"));
    if(fileName.isEmpty()) //if the file name is not empty
    {
        return false;
    }
    if(!fileName.endsWith(".txt"))
        fileName+=".txt";
    return this->saveFile(fileName);

}

void TextEditor::on_action_New_triggered()
{
    ui->textEdit->clear();
    ui->textEdit->setVisible(true);
}

bool TextEditor::loadFile(const QString &fileName)
{
    QFile file(fileName);
    if(! file.open(QFile::ReadOnly | QFile::Text))
    {
        QMessageBox::warning(this,tr("Warning:"),tr("can not read file %1,%2").arg(fileName)
                                                    .arg(file.errorString()));
        return false;
    }
     QTextStream in(&file);
     ui->textEdit->setText(in.readAll());
     this->curFile = QFileInfo(fileName).canonicalFilePath();
     ui->statusBar->showMessage(tr("load file %1").arg(this->curFile),2000);//2000ms later,statusbar will show
     this->setWindowTitle(curFile);
     return true;
}
void TextEditor::on_action_Open_triggered()
{
    if(this->okToContinue())
    {
        QString filename = QFileDialog::getOpenFileName(this,tr("Open text"),
                                                        tr("~/"),tr("Text files (*.txt)"));
        if(!filename.isEmpty())  //if filename is not empty
        {
            this->loadFile(filename);
        }
//        ui->textEdit->setVisible(true);
    }
}



void TextEditor::on_action_Save_triggered()
{
    this->save();
}

void TextEditor::on_action_SaveAs_triggered()
{
     this->saveAs();
}

void TextEditor::on_action_Quit_triggered()
{
    qApp->quit();
}

void TextEditor::on_actionAbout_triggered()
{
    QMessageBox::about(NULL,tr("About Text Editor"),
                       tr("<h2>Text Editor version 0.01</h2>"
                          "<p><h3>Copyright &copy;2010 software Inc.</h3></p>"
                          "<p>Text Editor is a samall application which is writed to study qt."
                          "<p>Author:David Luo E-mail:luodayuan2@gmail.com"));
}

void TextEditor::on_actionAboutQt_triggered()
{
    QMessageBox::aboutQt(NULL,tr("Qt Application"));
}

void TextEditor::on_action_Undo_triggered()
{
    ui->textEdit->undo();
}
/* menu "edit" implement*/
void TextEditor::on_action_Cut_triggered()
{
    ui->textEdit->cut();
}

void TextEditor::on_action_Copy_triggered()
{
    ui->textEdit->copy();
}

void TextEditor::on_action_Paste_triggered()
{
    ui->textEdit->paste();
}

void TextEditor::findText(const QString &text)
{
    if(!ui->textEdit->find(text))
    {
        QMessageBox::warning(this,tr("find next"),tr("can't find: %1").arg(text)/*,QMessageBox::Ok*/);
    }
}
void TextEditor::on_actionFind_triggered()
{
    if(!this->findDlg)
    {
        this->findDlg = new FindDlg(this);
        connect(findDlg,SIGNAL(findNext(QString)),this,SLOT(findText(QString)));
    }
    this->findDlg->show();
    this->findDlg->raise();
    this->activateWindow();
}

void TextEditor::on_actionSelect_All_triggered()
{
   ui->textEdit->selectAll();
}

void TextEditor::on_actionInsert_DateTime_triggered()
{
    QString dateTime = QDateTime::currentDateTime().toString("yyyy.MM.dd  hh.mm.ss dddd");
    ui->textEdit->insertPlainText(dateTime);
}
